//全局方法
export const HOOKS = [
  'beforeCreate',
  'created',
  'beforeMount',
  'mounted',
  'beforeUpdate',
  'updated',
  'beforeDestroy',
  'destroyed'
];
//策略模式
const starts = {}
starts.data = function (parentVal, childVal) {
  return childVal()
}
starts.methods = function () { }
starts.computed = function () { }
starts.watch = function () { }
//遍历生命周期
HOOKS.forEach(hook => {
  starts[hook] = mergeHook
})
function mergeHook(parentVal, childVal) {
  // { created:[a,b,c],mounted:[d,e,f] }
  if (childVal) {
    if (parentVal) {
      return parentVal.concat(childVal)
    } else {
      return [childVal]
    }
  } else {
    return parentVal
  }
}
export function mergeOptions(parent, children) { //第一次 {} {created:function a(){}}
  console.log(parent, children);

  // 最终需要 Vue.options ={ created:[a,b,c],mounted:[d,e,f] }
  const options = {}

  
  for (let key in parent) {
    mergeField(key)
  }
  for (let key in children) {
    mergeField(key)
  }
  function mergeField(key) {
    //根据key 策略模式
    if (starts[key]) { //
      options[key] = starts[key](parent[key], children[key])
    } else {
      options[key] = children[key]
    }
  }
  return options
}